/* eslint-disable */

//第三方类库加载管理js，方便切换lib


var configLibs = { 
	"hkws": [
		"hk/lib.js"
	],
	"wsocket": [
		"wsocket.js",
		"wsocket-client.js",
	],
	"md5": [
		"md5/md5.js"
	],
	"style": []
}


//内部处理方法
;
(function() {
	var r = new RegExp('(^|(.*?\\/))(include-lib.js)(\\?|$)'),
		s = document.getElementsByTagName('script'),
		targetScript
	for (var i = 0; i < s.length; i++) {
		var src = s[i].getAttribute('src')
		if (src) {
			var m = src.match(r)
			if (m) {
				targetScript = s[i]
				break
			}
		}
	}

	// cssExpr 用于判断资源是否是css
	var cssExpr = new RegExp('\\.css')

	function inputLibs(list, baseUrl, ix) {
		if (list == null || list.length === 0 || list.length <= ix) {
			return
		}
		var url = list[ix];
		if (!url.startsWith("http") && !url.startsWith("//:")) {
			url = baseUrl + url
		}
		let item;
		if (cssExpr.test(url)) {
			item = document.createElement("link");
			item.setAttribute("href", url);
			item.setAttribute("rel", "stylesheet");
		} else {
			item = document.createElement("script");
			item.setAttribute("src", url);
			item.setAttribute("type", "text/javascript");

		}
		document.head.appendChild(item);
		item.addEventListener("load", (e) => {
			inputLibs(list, baseUrl, ix + 1);
		});
	}

	var keys = {}
	//加载类库资源文件
	function load() {
		var arrInclude = (targetScript.getAttribute('include') || '').split(',')
		var libpath = targetScript.getAttribute('libpath') || ''
		if (libpath.lastIndexOf('/') !== libpath.length - 1) {
			libpath += '/'
		}
		//window.__abpath = libpath;

		for (var i = 0, len = arrInclude.length; i < len; i++) {
			var key = arrInclude[i]

			if (keys[key]) {
				//规避重复引入lib
				continue
			}
			keys[key] = true

			inputLibs(configLibs[key], libpath, 0)
		}
	}

	load()
})()